package com.game.springbootstart.controller.user;


import com.game.springbootstart.VO.FavoriteRequest;
import com.game.springbootstart.config.Result;
import com.game.springbootstart.service.IFavoritesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 喜欢表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-02-12
 */
@RestController
@RequestMapping("/favorites")
public class FavoritesController {

    @Autowired
    private IFavoritesService favoritesService;

    @PostMapping("/add")
    public Result addFavorite(@RequestBody FavoriteRequest favoriteRequest) {
        Long userId = favoriteRequest.getUserId();
        Long gameId = favoriteRequest.getGameId();

        // 调用服务方法将游戏添加到用户的最爱中
        boolean success = favoritesService.addFavorite(userId, gameId);
        if (success) {
            return Result.success("已加入我的最爱");
        } else {
            return Result.fail();
        }
    }

    @GetMapping("/list")
    public Result listFavorites(@RequestParam Long userId) {
        return Result.success(favoritesService.listBy(userId));
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody FavoriteRequest favoriteRequest) {
        Long userId = favoriteRequest.getUserId();
        Long gameId = favoriteRequest.getGameId();
        Boolean flag = favoritesService.delete(userId, gameId);
        if (flag) {
            return Result.success("成功删除");
        }else {
            return Result.fail();
        }
    }

    @PostMapping("/getLikedGames")
    public Result getLikedGames(@RequestParam Long userId) {
        return Result.success(favoritesService.selectFavorites(userId));
    }
}

